package com.lemniscate.audio_and_video_demo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.lemniscate.audio_and_video_demo.R;

/**
 * Created by Lemniscate on 2018/2/27.
 */

public class MyView extends View implements View.OnTouchListener {

    private Bitmap icon;

    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        rect = new Rect(0, 0, icon.getWidth(), icon.getHeight());

        this.setOnTouchListener(this);
    }

    private Rect rect;
    private Point point = new Point();
    private boolean canDrag = true;
    private int offsetX, offsetY = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.GRAY);
        canvas.drawBitmap(icon, rect.left, rect.top, null);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                point.x = (int) motionEvent.getX();
                point.y = (int) motionEvent.getY();
                if (rect.contains(point.x, point.y)) {
                    canDrag = true;
                    offsetX = point.x - rect.left;
                    offsetY = point.y - rect.top;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (canDrag) {
                    rect.left = (int) (motionEvent.getX() - offsetX);
                    rect.top = (int) (motionEvent.getY() - offsetY);
                    rect.right = rect.left + icon.getWidth();
                    rect.bottom = rect.top + icon.getHeight();

                    if (rect.left < 0) {
                        rect.left = 0;
                        rect.right = rect.left + icon.getWidth();
                    }
                    if (rect.right > getMeasuredWidth()) {
                        rect.right = getMeasuredWidth();
                        rect.left = rect.right - icon.getWidth();
                    }
                    if (rect.top < 0) {
                        rect.top = 0;
                        rect.bottom = rect.top + icon.getHeight();
                    }
                    if (rect.bottom > getHeight()) {
                        rect.bottom = getMeasuredHeight();
                        rect.top = rect.bottom - icon.getHeight();
                    }
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                canDrag = false;
                break;
        }
        return true;
    }
}
